<html>
<head><meta charset="utf-8"><title>Discussion on early-return from failed match? · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html">Discussion on early-return from failed match?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198453846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198453846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198453846">(May 22 2020 at 15:19)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> mentioned that there had been some discussion about adding a language feature to simplify code like this that combines control flow with destructuring:</p>
<div class="codehilite"><pre><span></span><code>fn fo() {
    let x = match bar {
        Some(v) =&gt; v,
        None =&gt; ... return
    };
}
</code></pre></div>


<p>Neither of us could find an open RFC on the topic, though. Does anyone remember such a discussion and know where it might be found?</p>



<a name="198459706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198459706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198459706">(May 22 2020 at 16:06)</a>:</h4>
<p>not an <em>open</em> RFC, but: <a href="https://github.com/rust-lang/rfcs/pull/1303">https://github.com/rust-lang/rfcs/pull/1303</a></p>



<a name="198467576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198467576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198467576">(May 22 2020 at 17:07)</a>:</h4>
<p><span class="user-mention" data-user-id="120076">@BatmanAoD (Kyle Strand)</span> We are currently considering reopening that discussion.</p>



<a name="198467696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198467696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198467696">(May 22 2020 at 17:08)</a>:</h4>
<p>There's renewed interest among the lang team in a <code>let</code>...<code>else</code> construct.</p>



<a name="198470262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198470262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198470262">(May 22 2020 at 17:29)</a>:</h4>
<p>Great! I had some thoughts on the subject yesterday, but want to read through what's already been proposed/discussed.</p>



<a name="198482950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198482950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198482950">(May 22 2020 at 19:15)</a>:</h4>
<p>Wow, that was long. I've added my two cents, though I'm not sure the end of that thread is really the best place for it. <a href="https://github.com/rust-lang/rfcs/pull/1303#issuecomment-632866111">https://github.com/rust-lang/rfcs/pull/1303#issuecomment-632866111</a></p>



<a name="198483169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483169">(May 22 2020 at 19:16)</a>:</h4>
<p>lol</p>



<a name="198483178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483178">(May 22 2020 at 19:17)</a>:</h4>
<p>you see why I brought this up as a place where</p>



<a name="198483193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483193">(May 22 2020 at 19:17)</a>:</h4>
<p>I would prefer for that iteration to be taking place in a <em>project group</em></p>



<a name="198483206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483206">(May 22 2020 at 19:17)</a>:</h4>
<p>indeed!</p>



<a name="198483207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483207">(May 22 2020 at 19:17)</a>:</h4>
<p>and hopefully with the first aim of "discovering the space of solutions"</p>



<a name="198483237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483237">(May 22 2020 at 19:17)</a>:</h4>
<p>to some extent that's already been done</p>



<a name="198483242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483242">(May 22 2020 at 19:17)</a>:</h4>
<p>so maybe it's "documenting" now</p>



<a name="198483273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483273">(May 22 2020 at 19:17)</a>:</h4>
<p>I believe my suggestion is actually novel, at least within that thread!</p>



<a name="198483275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483275">(May 22 2020 at 19:17)</a>:</h4>
<p>but I think it's an interesting case also of: </p>
<ul>
<li>we know the <em>problem</em></li>
<li>we have a few thoughts as to the solutions</li>
</ul>



<a name="198483289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198483289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198483289">(May 22 2020 at 19:17)</a>:</h4>
<p>(Which is not to say that's necessarily a good thing)</p>



<a name="198489258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198489258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198489258">(May 22 2020 at 20:09)</a>:</h4>
<p>It's certainly a novel solution. I'm not sure if it'd be preferable to <code>let pattern = expr else {}</code>, though.</p>



<a name="198489352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198489352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198489352">(May 22 2020 at 20:10)</a>:</h4>
<p>That would work for any pattern match, not just <code>Option</code> or <code>Result</code>.</p>



<a name="198493416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198493416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198493416">(May 22 2020 at 20:50)</a>:</h4>
<p>I guess I didn't make this explicit in my post, but with both <code>?{...}</code> and <code>expect</code>, <code>let pattern = expr else {...}</code> would be expressed as <code>let x = expr expect pattern ? {...}</code>.</p>



<a name="198521352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198521352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198521352">(May 23 2020 at 06:28)</a>:</h4>
<p>Yeah, that seemed clear from your post.</p>



<a name="198521359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198521359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198521359">(May 23 2020 at 06:29)</a>:</h4>
<p>At that point, though, it's not far off from just writing <code>let x = if let pattern = expr { x } else { return }</code>.</p>



<a name="198547992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/198547992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#198547992">(May 23 2020 at 19:15)</a>:</h4>
<p>The advantage I see is that <code>expect</code> and <code>?{}</code> could be implemented, stabilized, and used independently of each other. Though someone pointed out on the thread that <code>?{}</code> is already legal syntax.</p>



<a name="199415350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Discussion%20on%20early-return%20from%20failed%20match%3F/near/199415350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Discussion.20on.20early-return.20from.20failed.20match.3F.html#199415350">(Jun 01 2020 at 19:59)</a>:</h4>
<p><span class="user-mention" data-user-id="120076">@BatmanAoD (Kyle Strand)</span> There's also been discussion of it on IRLO in the form of <code>let x = v ☃ return;</code>, along with things like <code>let x = v ☃ 0;</code>, such as <a href="https://internals.rust-lang.org/t/something-for-coalescing-aka-generalized-improved-or-else/9295?u=scottmcm">https://internals.rust-lang.org/t/something-for-coalescing-aka-generalized-improved-or-else/9295?u=scottmcm</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>